﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace mdk.Cenas
{
    class Fase_1
    {

        GraphicsDevice graphicsDevice;
        ContentManager content;

        KeyboardState teclado, teclado_anterior;
        GamePadState controle, controle_anterior;

        Object3D block1, block2;        

        Camera camera;

        HUD hud;

        SkyDome skyDome;

        Boolean colidiu;

        private Viewport mapViewport;
        private Viewport defaultViewport;
        
        public Fase_1(ContentManager content, GraphicsDevice graphicsDevice)
        {
            this.content = content;
            this.graphicsDevice = graphicsDevice;

            skyDome = new SkyDome();

            LoadContent();
            ResetLevel();

            defaultViewport = graphicsDevice.Viewport;

            mapViewport = new Viewport(0, 0, graphicsDevice.Viewport.Width / 4, graphicsDevice.Viewport.Height / 4);

        }

        private void ResetLevel()
        {
            camera = new Camera(graphicsDevice);

            hud = new HUD(content);
            
        }

        private void LoadContent()
        {

            block1 = new Object3D(content.Load<Model>("caixa"));
            block2 = new Object3D(content.Load<Model>("caixa"));

            skyDome.LoadContent(graphicsDevice, content);
            
            
        }


        private void HandleInput()
        {

            teclado = Keyboard.GetState();            

            if (teclado.IsKeyDown(Keys.Space) && teclado_anterior.IsKeyUp(Keys.Space))
                camera.SwitchCameraMode();

            teclado_anterior = teclado;

            
        }

        public void Update(GameTime gameTime)
        {
            HandleInput();

            block1.HandleInput(gameTime);

            block1.Update(gameTime);
            block2.Update(gameTime);
           
            camera.Update(block1.World);

            colidiu = block2.HitBox.Intersects(block1.HitBox);            
                     
            hud.Update(gameTime);

            skyDome.Update(gameTime, camera);

        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {

            graphicsDevice.Viewport = defaultViewport;

            skyDome.Draw(gameTime, camera);
            
            block2.Draw(camera);
            block1.Draw(camera);
            
            hud.Draw(gameTime, spriteBatch);
            
            graphicsDevice.Viewport = mapViewport;
            
            block2.Draw(camera);
            block1.Draw(camera);
            
           
        }

        //private void DrawModel(Model model, Matrix worldMatrix)
        //{
        //    Matrix[] modelTransforms = new Matrix[model.Bones.Count];
        //    model.CopyAbsoluteBoneTransformsTo(modelTransforms);

            

        //    foreach (ModelMesh mesh in model.Meshes)
        //    {
        //        foreach (BasicEffect effect in mesh.Effects)
        //        {
        //            effect.EnableDefaultLighting();
        //            effect.World = modelTransforms[mesh.ParentBone.Index] * worldMatrix;
        //            effect.View = camera.viewMatrix;
        //            effect.Projection = camera.projectionMatrix;                    
                    
        //        }
        //        mesh.Draw();
        //    }

           

       // }

    }
}
